package top.kaoshanji.q1ch02c;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 代码清单-q1ch02c-26页a：父子容器继承关系
 * 阅读代码是从上往下，但是写代码却是从下往上
 * @author kaoshanji
 * ^_^
 * create time 2025/5/27 20:51
 */
public class Application {

    private ClassPathXmlApplicationContext parentContext;
    private ClassPathXmlApplicationContext childContext;

    // 构造器...给属性赋值..然后其他方法就可以使用调用
    public Application() {
        parentContext = new ClassPathXmlApplicationContext("q1ch02c/parent-beans.xml");
        childContext = new ClassPathXmlApplicationContext(new String[]{"q1ch02c/child-beans.xml"}, true, parentContext); ///// 66666

        parentContext.setId("parentContext");
        childContext.setId("childContext");
    }

    // 公共方法是功能..对外
    public void runTests() {
        testVisibility(parentContext, "parentHello");
        testVisibility(childContext, "parentHello");
        testVisibility(parentContext, "childHello");
        testVisibility(childContext, "childHello");
        testOverridden(parentContext, "hello");
        testOverridden(childContext, "hello");
    }

    public static void main(String[] args) {
        new Application().runTests();
    }

    // 私有方法是细节..对内
    private void testVisibility(ApplicationContext context, String beanName) {
        System.out.println(context.getId() + " can see " + beanName + ": " + context.containsBean(beanName));
    }

    private void testOverridden(ApplicationContext context, String beanName) {
        System.out.println("sayHello from " + context.getId() + ": " + context.getBean(beanName, Hello.class).sayHello());
    }

}
